<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="用户" prop="userId">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:apply:remove']"
          type="danger"
          plain
          icon="el-icon-circle-close"
          size="mini"
          @click="handleDelete"
        >拒绝</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:apply:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="序号" type="index" align="center" />
      <el-table-column label="用户" align="center" prop="nickName" />
      <el-table-column label="申请的部门或房间" align="center" prop="deptName" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:apply:edit']"
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="handleAgree(scope.row)"
          >同意</el-button>
          <el-button
            v-hasPermi="['system:apply:remove']"
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="handleDelete(scope.row)"
          >拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 同意对话框补全部门和角色对话框 -->
    <el-dialog title="同意审批" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="用户" prop="userId">
          <el-input v-model="form.nickName" placeholder="请输入用户" />
        </el-form-item>
        <el-form-item label="社区" prop="communityId">
          <el-input v-model="form.communityName" placeholder="请输入社区" />
        </el-form-item>
        <el-form-item label="部门 / 房间" prop="deptId">
          <el-input v-model="form.deptName" placeholder="请输入申请的部门或房间" readonly />
        </el-form-item>
        <el-form-item label="角色" prop="roleIds">
          <el-select v-model="form.roleIds" multiple placeholder="请选择">
            <el-option
              v-for="item in roleOptions"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId"
              :disabled="item.status == 1"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAgree">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listApply, getApply, delApply, addApply, updateApply, exportApply } from '@/api/system/apply'
import { getUser, addSelectUsers } from '@/api/system/user'

export default {
  name: 'Apply',
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户和社区关联表格数据
      applyList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 0：申请中，1：通过，2：拒绝字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        communityId: null,
        deptId: null,
        roleIds: null,
        postIds: null,
        status: '0',
        remarks: null
      },
      // 表单参数
      form: {
        userId: null,
        deptId: null,
        roleIds: []
      },
      // 表单校验
      rules: {
        userId: [
          { required: true, message: '用户不能为空', trigger: 'blur' }
        ],
        roleIds: [
          { required: true, message: '角色不能为空', trigger: 'blur' }
        ],
        deptId: [
          { required: true, message: '部门 / 住址不能为空', trigger: 'blur' }
        ],
      },
      // 该社区角色列表
      roleOptions: []
    }
  },
  created() {
    this.getList()
    this.getDicts('apply_status').then(response => {
      this.statusOptions = response.data
    })
  },
  methods: {
    /** 查询用户和社区关联列表 */
    getList() {
      this.loading = true
      listApply(this.queryParams).then(response => {
        this.applyList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 0：申请中，1：通过，2：拒绝字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        userCommId: null,
        userId: null,
        deptId: null,
        roleIds: null,
        postIds: null,
        status: null,
        remarks: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      console.log(this.ids)
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加用户和社区关联'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getApply(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改用户和社区关联'
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否拒绝申请?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delApply(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('已拒绝')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有用户和社区关联数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportApply(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    },
    // 点击同意按钮
    handleAgree(userComm) {
      console.log(userComm)
      // 获取用户的当前角色
      getUser(userComm.userId).then((response) => {
        this.form.userCommId = userComm.id
        this.form.nickName = userComm.nickName
        // this.postOptions = response.posts
        this.roleOptions = response.roles
        // this.form.postIds = response.postIds
        this.form.userId = userComm.userId
        this.form.roleIds = response.roleIds
        this.form.communityName = userComm.communityName
        this.form.deptId = userComm.deptId
        this.form.deptName = userComm.deptName
        this.open = true
      })
    },
    // 提交同意
    submitAgree() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          console.log(this.form)
          addSelectUsers(this.form).then(response => {
            this.msgSuccess('审批成功')
            this.open = false
            this.getList()
          })
        }
      })
    },
    // 取消同意审批
    cancelAgree() {
      this.reset()
      this.open = false
    }
  }
}
</script>
